

package com.eibus.applicationconnector.dotnet;

import junit.framework.TestCase;
import com.eibus.connector.nom.Connector;
import com.eibus.xml.nom.Node;

//@Author	: Lejil
//@Class	: DotnetDifferentTypesTest

/**
 *	<br/>Owner:			Lejil
 *	<br/>Description:	Invoke the .NET MetaData Methods for default values, different combinations of types etc.. 
 */
public class DotnetMetadataErrorTestCase extends TestCase
{
	Connector  connector;
	private final String assemblyName		=	"UnitTest, Version=2.0.0.0, Culture=neutral, PublicKeyToken=e25a327e965279f3";
	private final String isStatic			=	"false";
	private final String namesapce			=	"http://schemas.cordys.com/1.0/metadata/dotnet";
	
	protected void setUp()
	{
		try
		{
			connector = Connector.getInstance("DotNET Client");
			if (!connector.isOpen())
				connector.open();
		
		}
		catch(Exception exception)
		{
			fail("Could not get instance of Connector. "+exception.getMessage());
		}
	}
	
	protected void tearDown()
	{

	}

	public DotnetMetadataErrorTestCase(String name)
	{
		super(name);
	}
	
	public static void main(String[] args)
	{
		junit.textui.TestRunner.run(DotnetMetadataErrorTestCase.class);
	}
	
	


	public  boolean checkforSoapFault(int response) throws Exception
	{
		
		try
		{
			int  faultNode = XPathUtil.getSOAPFault(response);

			if ( faultNode == 0)
			{
				fail(".NET Connector Expected soapFault, but returned Incorrect result" );
			}
		}
		catch(Exception ex)
		{
			throw ex;
		}
		return true;
	}


	public void testMetadataGetAllStaticMethods() //1
	{
		int response =0,request =0;
		try
		{
			request 	= connector.createSOAPMethod(namesapce,"GetAllMethods");
			Node.createTextElement("assembly",assemblyName,request);
			Node.createTextElement("class","staticClassName",request);
			Node.createTextElement("isStatic",isStatic,request);
			response = connector.sendAndWait(Node.getParent(request));
			checkforSoapFault(response);
			//String result = Find.firstDataWithDefault(response,"?<SOAP:faultcode>","CorrectResult");
            String result = Node.getDataWithDefault(XPathUtil.getSOAPFaultcode(response), "No SOAP fault");
			if (!result.equals("Method.Invocation Error"))
			{
				//fail("testMetadataGetAllStaticMethods Expected value is  Method.Invocation Error , but retrunred " + result);
			}
			
		}
		catch(Exception ex)
		{
			fail ("GetAllStaticMethods failed with excption" + ex.toString());
		}
		finally
		{
			Util.delete(Node.getRoot(response));
			Util.delete(Node.getRoot(request));
			
		}
	
	}
	
	public void testMetadataGetAllInstanceMethods() //2
	{
		int response = 0,request =0;
		try
		{
			request 	= connector.createSOAPMethod(namesapce,"GetAllMethods");
			Node.createTextElement("assembly",assemblyName,request);
			Node.createTextElement("class","classNameInvalid",request);
			Node.createTextElement("isStatic",isStatic,request);
			response = connector.sendAndWait(Node.getParent(request));
			checkforSoapFault(response);
			//String result = Find.firstDataWithDefault(response,"?<SOAP:faultcode>","CorrectResult");
            String result = Node.getDataWithDefault(XPathUtil.getSOAPFaultcode(response), "No SOAP fault");
			if (!result.equals("Method.Invocation Error"))
			{
				//fail("testMetadataGetAllInstanceMethods Expected value is  Method.Invocation Error , but retrunred " + result);
			}
		}
		catch(Exception ex)
		{
		  fail("testMetadataGetAllInstanceMethods failed with exception");	
		}
		finally
		{
			Util.delete(Node.getRoot(response));
			Util.delete(Node.getRoot(request));
		}
	}
	
	
	public void testMetadataGetAllRefMethods() //3
	{
		int response =	0 ,request =0;
		try
		{
			request 	= connector.createSOAPMethod(namesapce,"GetAllMethods");
			Node.createTextElement("assembly",assemblyName,request);
			Node.createTextElement("class","refClassName",request);
			Node.createTextElement("isStatic",isStatic,request);
			response = connector.sendAndWait(Node.getParent(request));
			checkforSoapFault(response);
            String result = Node.getDataWithDefault(XPathUtil.getSOAPFaultcode(response), "No SOAP fault");
			if (!result.equals("Method.Invocation Error"))
			{
				//fail("testMetadataGetAllRefMethods Expected value is  Method.Invocation Error , but retrunred " + result);
			}

		}
		catch(Exception ex)
		{
			 fail("testMetadataGetAllRefMethods failed with exception");
		}
		finally
		{
			Util.delete(Node.getRoot(response));
			Util.delete(Node.getRoot(request));
			
		}
	}
	
	public void testMetadataGetAllRefStaticMethods() //4
	{
		int response = 0,request =0;
		try
		{
			request 	= connector.createSOAPMethod(namesapce,"GetAllMethods");
			Node.createTextElement("assembly","assemblyName",request);
			Node.createTextElement("class","staticRefClassName",request);
			Node.createTextElement("isStatic",isStatic,request);
			response = connector.sendAndWait(Node.getParent(request));
			checkforSoapFault(response);
			//String result = Find.firstDataWithDefault(response,"?<SOAP:faultcode>","CorrectResult");
            String result = Node.getDataWithDefault(XPathUtil.getSOAPFaultcode(response), "No SOAP fault");
			if (!result.equals("Method.Invocation Error"))
			{
				//fail("testMetadataGetAllRefStaticMethods Expected value is  Method.Invocation Error , but retrunred " + result);
			}
		}
		catch(Exception ex)
		{
			 fail("testMetadataGetAllRefStaticMethods failed with exception");
		}
		finally
		{
			Util.delete(Node.getRoot(response));
			Util.delete(Node.getRoot(request));
		}
	}
			
}
